package da.metricscraft.client.frames.layers;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Random;

import da.metricscraft.client.frames.GraphicsPanel;
import da.metricscraft.client.threads.SpatialXMLLoader;
import da.metricscraft.client.threads.SpatialXMLLoader.LocationStruct;
import da.metricscraft.client.threads.SpatialXMLLoader.MoveActions;

public class MovementLayer extends Layer<HashMap<String, ArrayList<SpatialXMLLoader.LocationStruct>>> {
	private static MovementLayer singleton;
	private HashMap<String, Color> players = new HashMap<String, Color>();
	private Random r = new Random();
	private boolean runOnce = false;
	private ArrayList<String> activePlayers;
	public static MovementLayer getInstance(){
		return singleton;
	}
	public MovementLayer(String name, Graphics g, Point size){
		super(name, g, size);
		activePlayers = new ArrayList<String>();
		singleton = this;
	}
	public void showPlayer(String player){
		if (!activePlayers.contains(player)){
			activePlayers.add(player);
			update(true);
		}
	}
	public void hidePlayer(String player){
		if (activePlayers.contains(player)){
			activePlayers.remove(player);
			update(true);
		}
	}
	@Override
	public void update(boolean draw) {
		// TODO Auto-generated method stub
		if (data != null){
			Graphics g = null;
			if (draw){
				layerImage = new BufferedImage((int)size.getX(), (int)size.getY(), BufferedImage.TYPE_INT_ARGB);
				g = layerImage.getGraphics();
				g.setColor(new Color(0f,0f,0f,0.7f));
				g.fillRect(0, 0, layerImage.getWidth(), layerImage.getHeight());
			}
			Color c = new Color(0xaa, 0, 0, 0x99);
			Point prev = null;
			int[] posMod = { GraphicsPanel.getInstance().getLowest().x, GraphicsPanel.getInstance().getLowest().y};
			
			for (Entry<String, ArrayList<SpatialXMLLoader.LocationStruct>> m : data.entrySet()){
				if (!runOnce) activePlayers.add(m.getKey());
				if (!activePlayers.contains(m.getKey())) continue;
				String player = m.getKey();
				ArrayList<SpatialXMLLoader.LocationStruct> positions = m.getValue();
				System.out.println("[MovementLayer][update] drawing path for " + player);
				if (!players.containsKey(player)){
					players.put(player, new Color((int)(r.nextDouble() * 0x66000000 + r.nextDouble() * 0x00660000 + r.nextDouble() * 0x00006600 + 0x999999ff)));
				}
				c = players.get(player);
				if (draw)
					g.setColor(c);
				Collections.sort(positions);
				
				for(LocationStruct entry : positions){
					if (entry.getAction() == MoveActions.MOVE){
						
						Point p = entry.getPosition();
						if (draw)
							drawPositionSquare(g, prev, posMod, 0, p, c);
						prev = p;
					}
				}
				prev = null;
			}
			runOnce = true;
			GraphicsPanel.getInstance().repaint();
		}
	}
	private void drawPositionSquare(Graphics g, Point prev, int[] posMod, int playeroffset, Point p, Color prevColor) {
		int[] blocksize = GraphicsPanel.BLOCKSIZE;
		if (prev == null || p.distance(prev) > blocksize[0]*2){
			prev = p;
			g.setColor(new Color(0xff0000));
		}
		else if (!prev.equals(p)) g.setColor(prevColor);
		g.fillRect(blocksize[0] * (p.x - posMod[0]) + playeroffset - blocksize[0]/4, blocksize[1] * (p.y - posMod[1]) + playeroffset - blocksize[0]/4,
				blocksize[0]/2, blocksize[1]/2);
		g.drawLine(blocksize[0] * (prev.x - posMod[0]) + playeroffset, blocksize[1] * (prev.y - posMod[1]) + playeroffset, 
				blocksize[0] * (p.x - posMod[0]) + playeroffset, blocksize[1] * (p.y - posMod[1]) + playeroffset);
	}

}
